﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using blackhouse.Aparts.Web.Modules;
using blackhouse.BlackApart.Business.Apartments;
using blackhouse.BlackApart.Web;
using blackhouse.mojoPortalExtend;
using blackhouse.mojoPortalExtend.Breadcrumbs;
using mojoPortal.Web;

namespace blackhouse.Aparts.Web.Pages {
    public partial class Gallery : NonCmsBasePage {

        #region Fields

        private bool useDynamic;
        private BreadcrumbsAccess breadcrumbs;

        #endregion

        #region Event Handlers

        protected override void OnPreInit(EventArgs e) {
            this.AllowSkinOverride = true;
            HttpContext.Current.Items.Add("skin", "blackhouse");
            HttpContext.Current.Items.Add("short-top", true);


            base.OnPreInit(e);
        }

        protected override void OnInit(EventArgs e) {
            base.OnInit(e);

            this.useDynamic = this.Request["display"] == "dynamic";
        }

        protected override void OnLoad(EventArgs e) {
            base.OnLoad(e);
                
            this.AddClassToBody("withFrontBaner");
            this.AddClassToBody("loader");

            this.breadcrumbs = new BreadcrumbsAccess();

            this.rptApartments.DataSource = blackhouse.BlackApart.Business.Factory.Instance().GetApartmentGroup().GetAllApartmentTypes().OrderBy(g => g.ShortName);
            this.rptApartments.DataBind();

            if (!this.useDynamic) {
                this.pnlTryRedirect.Visible = true;
            }

            PageProvider p = new PageProvider();
            p.SetCanonicalUrl(UrlUtils.LocalizeUrl("/" + GetGlobalResourceObject("Aparts", "galleryUrl").ToString()));
            p.SetPageTitle(GetGlobalResourceObject("Aparts", "galleryTitle").ToString());
            p.SetPageDescription(GetGlobalResourceObject("Aparts", "galleryDesc").ToString());

            this.breadcrumbs.AddCrumb("Galeria zdjęć", UrlUtils.LocalizeUrl("/" + GetGlobalResourceObject("Aparts", "galleryUrl").ToString()));

        }

        #endregion

        #region Methods

        protected void rptApartments_ItemDataBound(object sender, RepeaterItemEventArgs e) {
            switch (e.Item.ItemType) {
                case ListItemType.Item:
                case ListItemType.AlternatingItem:

                    IApartmentGroup group = e.Item.DataItem as IApartmentGroup;
                    ApartmentGallery gallery = e.Item.FindControl("gallery") as ApartmentGallery;

                    if (group != null && gallery != null) {
                        gallery.UseJavascript = this.useDynamic;
                        gallery.Populate(group);
                    }


                    break;
            }
        }

        #endregion

    }
}